package com.igeek.boot3_web_request.boot.controller;

import com.igeek.boot3_web_request.boot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;

/**
 * @Author: zhiyu
 * @Date:14:24
 * @DESC:
 */
@Controller
//@RequestMapping("/rest")
public class ParamController {
    @GetMapping("/path/{id}/{name}")
    @ResponseBody
    public Map<String,Object> testPathVariable(
            @PathVariable("id") int id,
            @PathVariable("name") String username,
            @PathVariable Map<String,Object> pv){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",username);
        map.put("pv",pv);
        return map;
    }


    //========@RequestHeader 获取请求头========
    //要求：使用@RequestHeader("key") 根据key获取请求头中的对应的值
    @GetMapping("/head")
    @ResponseBody
    public Map<String,Object> testRequestHeader(
            @RequestHeader("Host") String host,
            @RequestHeader("token") String token
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("host",host);
        map.put("token",token);
        return map;
    }
    @GetMapping("/addCookie")
    public String addCookie(HttpServletResponse response){
        Cookie cookie1 = new Cookie("name","zhangsan");
        Cookie cookie2 = new Cookie("password","123456");
        cookie1.setMaxAge(24*60*60);
        cookie2.setMaxAge(24*60*60);
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        //3.请求转发到 /getCookie 请求地址  forward:/getCookie
        //3.重定向到 /getCookie 请求地址  redirect:/getCookie
        return "redirect:/getCookie";
    }
    @GetMapping("/getCookie")
    @ResponseBody
    public Map<String,Object> getCookie(
            @CookieValue(value = "username",required = false,defaultValue = "") String name,
            @CookieValue(value = "password",required = false,defaultValue = "") String pwd
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",name);
        map.put("password",pwd);
        return map;
    }
    //========@RequestParam 获取请求参数========
    /*
     * @RequestParam 获取请求参数
     * 1.地址栏追加请求数据 url?name=zs&age=20&hobby=1&hobby=2
     * 2.表单数据        <input type="" name="key"> key代表的是请求参数名称
     * 3.如果想要一次性获取所有请求参数(包含同名的多个请求参数) 使用MultiValueMap
     * */
    @GetMapping("/requestparam")
    @ResponseBody
    public Map<String,Object> testRequestParam(
            @RequestPart MultipartFile file,
            @RequestParam(name = "name",required = false,defaultValue = "admin") String name,
            @RequestParam(name = "age",required = false,defaultValue = "18") int age,
            @RequestParam(name = "hobby") String[] hobby,
            @RequestParam MultiValueMap<String,Object> pv
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("age",age);
        map.put("hobby", Arrays.toString(hobby));
        map.put("pv", pv);
        return map;
    }
    //========@RequestAttribute 获取request域属性========
    /*
     * Servlet 中的四大作用域: 存储共享数据（适用于前后端不分离）
     *   application（ServletContext）> session > request > page
     * */

    /*添加请求属性的数据*/
    @GetMapping("/addAttribute")
    public String testAddRequestAttribute(HttpServletRequest request){
        request.setAttribute("msg","hello Attribute");
        request.setAttribute("username","root");
        return "redirect:/getAttribute";
    }
    /*
   获取请求属性中的数据
       @RequestAttribute（“key”） 等价于 request.getAttribute("key");
   */
    @GetMapping("/getAttribute")
    @ResponseBody
    public Map<String,Object> testGetRequestAttribute(
            HttpServletRequest request,
            @RequestAttribute(value = "msg",required = false) String msg2,
            @RequestAttribute(value = "username",required = false) String username){
        Object msg1 = request.getAttribute("msg");
        HashMap<String, Object> map = new HashMap<>();
        map.put("request&msg1",msg1);
        map.put("@RequestAttribute&msg2",msg2);
        map.put("username",username);
        return map;
    }


    //
    //========@RequestBody 获取请求体[POST]========
    /*
     * @RequestBody : 将json格式的请求数据 转换为 java对象
     * @ResponseBody: 将要想要的结果java对象 转换为 json格式数据
     *
     * vue+v-model ==> 请求数据json
     * */
    @GetMapping("/getBody")
    @ResponseBody
    public User testRequestBody(@RequestBody User user){
        return user;
    }
}
